package com.cw.util;

import com.ewell.sdk.business.EwellServiceTool;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

@Data
@AllArgsConstructor
public class MQMsg {
    private String userName;        // 设定消息发送名称
    private String password;
    
    private String patName; // 患者姓名 必须
    private String patIndexNo;  // 患者索引号 必须
    private String outHospIndexNo = "0";    // 门诊索引号
    private String inHospIndexNo = "0";     // 住院索引号
    private String outHospNo = "0";         // 门诊流水号
    private String inHospNo = "0";          // 住院流水号
    private String msgType = "床位通知电子化病历";
    private String msgTitle;                 // 消息主题
    private String msgContent;              // 消息内容
    private String senderCode;              // 发送人工号
    private String senderName;              // 发送人姓名
    private String createTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    private String reciveCode;              // 接收人工号
    private String reciveName;              // 接收人姓名
    private String sysUrl = "0";
    private String messageType;  //消息类型 “0全院”/“1科室”/“2个人”
    private String bedNum;                  // 床位号
    private String deptCode;
    private String deptName;
    private String wardCode;
    private String wardName;
    private String appId = "0";
    private String hospDistrictCode;
    private String hostDistrictName;
    private String OIInd = "0";
    private String queueManagerName;
    private String channel;
    private EwellServiceTool ewellServiceTool;

    public MQMsg() {
        if (this.ewellServiceTool == null){
            this.ewellServiceTool = new EwellServiceTool();
        }
    }

    public String createXMLString(MQMsg mqMsg) {
        StringBuilder stringBuilder = new StringBuilder();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String dateFormat = simpleDateFormat.format(date);
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                "" +
                "<ESBEntry>" +
                "    <AccessControl>" +
                "        <UserName>"+mqMsg.userName+"</UserName>" +
                "        <Password>"+mqMsg.password+"</Password>" +
                "        <Fid>"+mqMsg.channel+"</Fid>" +
                "        <OrderNo>PS35035S35001</OrderNo>" +
                "        <SysFlag>1</SysFlag>" +
                "    </AccessControl>" +
                "    <MessageHeader>" +
                "        <Fid>"+mqMsg.channel+"</Fid>" +
                "        <OrderNo>PS35035S35001</OrderNo>" +
                "        <SourceSysCode>"+mqMsg.userName+"</SourceSysCode>" +
                "        <TargetSysCode>S35</TargetSysCode>" +
                "        <HospCode>GH01</HospCode>" +
                "        <ReturnFlag>-1</ReturnFlag>" +
                "        <MsgDate>"+dateFormat+"</MsgDate>" +
                "    </MessageHeader>" +
                "    <MsgInfo>" +
                "        <Msg id=\"1\" lastUpdate=\"" + dateFormat + "\" action=\"insert\">" +
                "            <PAT_NAME>" + mqMsg.patName + "</PAT_NAME>" +
                "            <PAT_INDEX_NO>" + mqMsg.patIndexNo + "</PAT_INDEX_NO>" +
                "            <OUTHOSP_INDEX_NO>" + mqMsg.outHospIndexNo + "</OUTHOSP_INDEX_NO>" +
                "            <INHOSP_INDEX_NO>" + mqMsg.inHospIndexNo + "</INHOSP_INDEX_NO>" +
                "            <OUTHOSP_NO>" + mqMsg.outHospNo + "</OUTHOSP_NO>" +
                "            <INHOSP_NO>" + mqMsg.inHospNo + "</INHOSP_NO>" +
                "            <MSG_TYPE>" + mqMsg.msgType + "</MSG_TYPE>" +
                "            <MSG_TITLE>" + mqMsg.msgTitle + "</MSG_TITLE>" +
                "            <MSG_CONTENT>" + mqMsg.msgContent + "</MSG_CONTENT>" +
                "            <SENDER_CODE>" + mqMsg.senderCode + "</SENDER_CODE>" +
                "            <SENDER_NAME>" + mqMsg.senderName + "</SENDER_NAME>" +
                "            <CREATE_TIME>" + mqMsg.createTime + "</CREATE_TIME>" +
                "            <RECIVE_CODE>" + mqMsg.reciveCode + "</RECIVE_CODE>" +
                "            <RECIVE_NAME>" + mqMsg.reciveName + "</RECIVE_NAME>" +
                "            <SYS_URL>" + mqMsg.sysUrl + "</SYS_URL>" +
                "            <BED_NUM>" + mqMsg.bedNum + "</BED_NUM>" +
                "            <DEPT_CODE>" + mqMsg.deptCode + "</DEPT_CODE>" +
                "            <DEPT_NAME>" + mqMsg.deptName + "</DEPT_NAME>" +
                "            <WARD_CODE>" + mqMsg.wardCode + "</WARD_CODE>" +
                "            <WARD_NAME>" + mqMsg.wardName + "</WARD_NAME>" +
                "            <APP_ID>" + mqMsg.appId + "</APP_ID>" +
                "            <HOSP_DISTRICT_CODE>" + mqMsg.hospDistrictCode + "</HOSP_DISTRICT_CODE>" +
                "            <HOSP_DISTRICT_NAME>" + mqMsg.hostDistrictName + "</HOSP_DISTRICT_NAME>" +
                "            <OI_IND>" + mqMsg.OIInd + "</OI_IND>" +"<MESSAGE_TYPE>"+mqMsg.messageType+"</MESSAGE_TYPE>"+
                "        </Msg>" +
                "    </MsgInfo>" +
                "</ESBEntry>");
        return stringBuilder.toString();
    }

    public String putMsg(String msg) {
        String connect = null;
        String msgId = "";
        try {
            connect = ewellServiceTool.connect(this.queueManagerName);
            msgId = ewellServiceTool.putMsg(connect, this.channel, msg);
            ewellServiceTool.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return msgId;
    }
}
